<?php
namespace api\controller;
use common\controller\VerifyController;
use common\service\ReportsService;
use think\Request;
use think\Response;
/**
 * 报表控制器
 */
class ReportsController extends VerifyController
{
    private ReportsService $reportsService;

    public function __construct()
    {
        parent::__construct();
        $this->reportsService = ReportsService::getInstance();
    }
    /**
     * 报表-原计划交付的节点数据
     * @param \think\Request $request
     * @return Response|void
     */
    public function getDeliveryPlanNodeTaskIds(Request $request)
    {
        $param = $request->param()['param'];

        $projectId = $param['project_id'] ?? '';
        $rangTime  = $param['time_range'] ?? [];
        [$startTime, $endTime] = $rangTime;
        $page = $param['page'] ?? [1,50];
        $tenantId  = $request->getTenantId();
        $res = $this->reportsService->getDeliveryPlanNodeTaskIds($projectId, $tenantId, $startTime, $endTime,$page);

        return json(success_response('OK', $res));

    }

    /**
     * 报表-已交付的节点数据
     *
     * @param \think\Request $request
     *
     * @return Response|void
     */
    public function getDeliveredTaskIds(Request $request)
    {
        $param = $request->param()['param'];
        $projectId = $param['project_id'] ?? '';
        $rangTime  = $param['time_range'] ?? [];
        [$startTime, $endTime] = $rangTime;
        $page = $param['page'] ?? [1,50];
        $tenantId  = $request->getTenantId();
        $res = $this->reportsService->getDeliveredTaskIds($projectId, $tenantId, $startTime, $endTime,$page);

        return json(success_response('OK', $res));

    }

    /**
     * 报表-按指定时间范围内的工单数量
     * @param \think\Request $request
     *
     * @return \think\Response
     */
    public function getUnhandleWorkOrderCount(Request $request)
    {
        $param = $request->param()['param'];
        $projectId = $param['project_id'] ?? '';
        $rangTime  = $param['time_range'] ?? [];
        [$startTime, $endTime] = $rangTime;
        $tenantId  = $request->getTenantId();
        $res = $this->reportsService->getUnhandleWorkOrderCount($projectId, $tenantId, $startTime, $endTime);

        return json(success_response('query success', $res));
    }

	/**
	 * 报表数据-工单数据
	 * @param \think\Request $request
	 *
	 * @return \think\Response
	 */
    public function getUnhandleWorkOrderList(Request $request): Response
	{
        $param = $request->param()['param'];
        $projectId = $param['project_id'] ?? '';
        $rangTime  = $param['time_range'] ?? [];
        [$startTime, $endTime] = $rangTime;
        $page = $param['page'] ?? [1,50];
        $tenantId  = $request->getTenantId();

        $res = $this->reportsService->getUnhandleWorkOrderList($param,$projectId, $tenantId, $startTime, $endTime,$page);

        return json(success_response('query success', $res));

    }
}
